﻿using System.Diagnostics;
using System;
using System.Linq;
using System.Web.Mvc;
using Sharp.Infrastructure;
using Sharp.Portal.Mvc.Form;

namespace Sharp.Portal.Mvc.Filter
{
    [DebuggerStepThrough]
    public class ViewEnginesResultFilter : IResultFilter, IExceptionFilter
    { 
        public void OnResultExecuted(ResultExecutedContext filterContext)
        {
        }

        public void OnResultExecuting(ResultExecutingContext filterContext)
        {
            var request = filterContext.RequestContext;
            var viewData = filterContext.Controller.ViewData;

            var baseViewModel = viewData.Model as ISharpForm;

            if (baseViewModel != null)
            {
                var page = baseViewModel.UI;
                if (page.Title.IsNotNullOrEmpty())
                {
                    return;
                }
                if (request.RouteData.Values["action"].ToString() == "Create")
                {
                    page.Title = "新增";
                }
                else if (request.RouteData.Values["action"].ToString() == "Details")
                {
                    page.Title = "详情";
                }
                else if (request.RouteData.Values["action"].ToString() == "Edit")
                {
                    page.Title = "编辑";
                }
            }
        }
         
        public void OnException(ExceptionContext filterContext)
        {
            //SetupSkinViewEngines(filterContext.RequestContext, filterContext.Controller.ViewData, filterContext.Result as ViewResultBase);
        }
         
    }
}
